﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALBase;
using OneMile.Entity.Model;
using System.Collections;
using System.Data;
using OneMile.Common;

namespace OneMile.DAL
{
    public class DALUPromotions:BaseDAL
    {
        public DBAccess db = DBConnection.CreateDB();

        public IList<EPromotions> GetPromotionsByShopId(String shopsId, List<String> idList)
        {
            try
            {

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" SELECT [PromotionsId], [PromotionsType],[ShopsId],[PromotionsTitle],[Discount],[ConditionPrice],[OffsetPrice],[CommodityId],[StartDate],[EndDate]");            
                sb.AppendLine("	  FROM Fct_Promotions ");
                sb.AppendLine("	 WHERE ShopsId=@ShopsId and (PromotionsType IN (1,3)) and Disabled = 0 and (GETDATE() BETWEEN StartDate and DATEADD(DAY,1,EndDate))");
                
                if (idList != null && idList.Count != 0)
                {
                    string commodityIds = "";
                    foreach (var id in idList)
                    {
                        commodityIds += String.Format("'{0}',", id);
                    }
                    commodityIds = commodityIds.Substring(0, commodityIds.Length - 1);

                    sb.AppendLine("             UNION ");
                    sb.AppendLine("             SELECT [PromotionsId], [PromotionsType],[ShopsId],[PromotionsTitle],[Discount],[ConditionPrice],[OffsetPrice],[CommodityId],[StartDate],[EndDate]");
                    sb.AppendLine("			      FROM  Fct_Promotions ");
                    sb.AppendLine(String.Format("WHERE (CommodityId IN ({0})) and Disabled = 0 and (GETDATE() BETWEEN StartDate and DATEADD(DAY,1,EndDate))", commodityIds));
                }


                Hashtable hs = new Hashtable();
                hs.Add("ShopsId", shopsId);
                DataTable dt = db.ExecuteQuery(sb.ToString(), hs);
                IList<EPromotions> list = null;
                if (dt.Rows.Count > 0)
                {
                    list = new List<EPromotions>();
                    EPromotions promotions = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        promotions = new EPromotions();
                        Guid promotionsId = new Guid(dt.Rows[0]["PromotionsId"].TString());
                        Guid shopId = new Guid(dt.Rows[i]["ShopsId"].TString());
                        promotions.PromotionsId = promotionsId;
                        promotions.ShopsId = shopId;
                        promotions.PromotionsType = dt.Rows[i]["PromotionsType"].TInt32();
                        promotions.PromotionsTitle = dt.Rows[i]["PromotionsTitle"].TString();
                        promotions.Discount = dt.Rows[i]["Discount"].TDecimal();
                        promotions.ConditionPrice = dt.Rows[i]["ConditionPrice"].TDecimal();
                        promotions.OffsetPrice = dt.Rows[i]["OffsetPrice"].TDecimal();
                        Guid commodityId = dt.Rows[i]["CommodityId"].TString() == "" ? Guid.Empty : new Guid(dt.Rows[i]["CommodityId"].TString());
                        promotions.CommodityId = commodityId;
                        promotions.StartDate = dt.Rows[i]["StartDate"].TDateTime();
                        promotions.EndDate = dt.Rows[i]["EndDate"].TDateTime();
                        list.Add(promotions);
                    }
                }
                return list;
            }
            catch (Exception e)
            {
                
                base.WriteLog(e.Message);
                return null;
            }
        }
    }
}
